<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC2117 – To run commands as another user, use `su -c` or `sudo`.</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC2117 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC2117">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2 id="to-run-commands-as-another-user-use-su--c-or-sudo">To run
commands as another user, use <code>su -c</code> or
<code>sudo</code>.</h2>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC2117.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">whoami</span></span>
<span id="cb1-2"><a href="SC2117.html#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">su</span></span>
<span id="cb1-3"><a href="SC2117.html#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="fu">whoami</span></span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC2117.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="fu">whoami</span></span>
<span id="cb2-2"><a href="SC2117.html#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="fu">sudo</span> whoami</span></code></pre></div>
<h3 id="rationale">Rationale:</h3>
<p>It's commonly believed that <code>su</code> makes a session run as
another user. In reality, it starts an entirely new shell, independent
of the one currently running your script.</p>
<p><code>su; whoami</code> will start a root shell and wait for it to
exit before running <code>whoami</code>. It will not start a root shell
and then proceed to run <code>whoami</code> in it.</p>
<p>To run commands as another user, use <code>sudo some command</code>
or <code>su -c 'some command'</code>. <code>sudo</code> is preferred
when available, as it doesn't require additional quoting and can be
configured to run passwordless if desired.</p>
<h3 id="exceptions">Exceptions</h3>
<p>If you're aware of the above and want to e.g. start an interactive
shell for a user, feel free to ignore this message.</p>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


